import Ember from 'ember';
import Changeset from 'ember-changeset';
import SalarypositionsValidations from '../../../validations/salarypositions';
import GoodsValidations from '../../../validations/goods';
import lookupValidator from 'ember-changeset-validations';

export default Ember.Controller.extend(SalarypositionsValidations,GoodsValidations, {
    constants: Constants,
    detailEdit: false,
    delFlag: true,
    dateService: Ember.inject.service("date-service"),
    dataLoader:Ember.inject.service('data-loader'),
    store: Ember.inject.service("store"),
    isAdmin: "0",
    salarypositionModel:Ember.computed("salarypositionsInfo",function(){
      var model = this.get("salarypositionsInfo");
      if (!model) {
        return null;
      }
      return new Changeset(model, lookupValidator(SalarypositionsValidations), SalarypositionsValidations);
    }),
    salarypositionModelObs:function(){
      var model = this.get("salarypositionsInfo");
      if (!model) {
        return null;
      }
      let salarypositionModel = new Changeset(model, lookupValidator(SalarypositionsValidations), SalarypositionsValidations);
      this.set('salarypositionModel',salarypositionModel);
    },
    actions: {
        choosewithSalary() {
          if(!this.get("detailEdit")) {
            return;
          }
          let isAdmin = this.get("isAdmin");
          if(isAdmin == "1"){
            this.set("isAdmin","0");
          } else {
            this.set("isAdmin","1");
          }
        },
        invalid() {
            //alert("invalid");
        },
        invitation(){
          this.set('addNewGoodsModel',false);
        },
        //编辑
        detailEditClick(){
          this.set('detailEdit',true);
          this.salarypositionModelObs();
        },
        //删除
        delById(){
          var _self = this;
          App.lookup('controller:business.mainpage').showConfirm("是否确定删除此记录",function(){
            _self.send('cancelPassSubmit',_self.get('salarypositionModel'));
          });
        },
        cancelPassSubmit(salaryposition){
          App.lookup('controller:business.mainpage').openPopTip("正在删除");
          this.set("showpopInvitePassModal",false);
          salaryposition.set("delStatus", 1);
          salaryposition.save().then(function() {
            App.lookup('controller:business.mainpage').showPopTip("删除成功");
            var mainpageController = App.lookup('controller:business.mainpage');
            mainpageController.switchMainPage('salary-base');
          },function(err){
            let error = err.errors[0];
            if(error.code === '14'){
              App.lookup('controller:business.mainpage').showAlert('此岗位角色已经使用，不能删除');
            }
          });
        },
        //保存采购信息
        savesalaryposition(){
          let _self = this;
          var isAdmin = this.get("isAdmin");
          let salarypositionModel = this.get('salarypositionModel');
          let editMode = this.get('editMode');
          salarypositionModel.set('isAdmin',isAdmin);//保存是否是管理员
          salarypositionModel.validate().then(function(){
            if(salarypositionModel.get('errors.length')===0){
              App.lookup('controller:business.mainpage').openPopTip("正在保存");
              salarypositionModel.save().then(function(){
                App.lookup('controller:business.mainpage').showPopTip("保存成功");
                if(editMode == 'edit'){
                  _self.set('detailEdit',false);
                  var route=App.lookup('route:business.mainpage.salary-base-detail');
                  route.refresh();//刷新页面
                }else if(editMode == 'add'){
                  var mainpageController = App.lookup('controller:business.mainpage');
                  mainpageController.switchMainPage('salary-base');
                }
              },function(err){
                App.lookup('controller:business.mainpage').showPopTip("保存失败");
              });
            }else{
              salarypositionModel.set("validFlag",Math.random());
            }
          });
        },
        //取消
        detailCancel(){
          let editMode = this.get('editMode');
          if(editMode == 'edit'){
            this.get('salarypositionsInfo').rollbackAttributes();
            this.set('detailEdit',false);
            var route=App.lookup('route:business.mainpage.salary-base-detail');
            route.refresh();//刷新页面
          }else if(editMode == 'add'){
            var mainpageController = App.lookup('controller:business.mainpage');
            mainpageController.switchMainPage('salary-base');
          }
        },
    }
});
